package coroutines

import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking

var winner = ""

fun main() = runBlocking {
    GlobalScope.launch { run("兔子") }
    GlobalScope.launch { run("乌龟") }

    delay(60000)
}

private suspend fun run(name: String) {
    for (i in 1..100) {
        if (i >= 100) {
            winner = name
            println(winner + "获胜了!")
            break
        } else if (winner.isNotEmpty()) {
            break
        }
        if ("乌龟" == name) delay(10)
        println("$name -> 跑了$i 步")
        if (i > 95 && "兔子" == name) delay(1000)
    }
}
